package com.mdlib.helper.ui.fragments.sect.vicinity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ljlib.core.base.BaseFragment;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.model.AppModel;
import com.mdlib.helper.model.entity.NearRunEntity;
import com.mdlib.helper.model.entity.RunEntity;
import com.mdlib.helper.ui.adapter.VicinityRunAdapter;
import com.mdlib.helper.ui.widget.VicinityRunDataEngine;
import com.mdlib.helper.ui.widget.springview.DefaultFooter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/14 14:32
 * 邮箱：hqy_xz@126.com
 */

public class VicinityRunFragment extends BaseFragment {


    @Bind(R.id.rv_vicinity_run)
    RecyclerView mRvVicinityRun;
    @Bind(R.id.sv_send_run)
    SpringView mSvSendRun;

    private int pageNumber = 1;

    private List<RunEntity> runList = new ArrayList<>();

    private VicinityRunAdapter mRunAdapter;
    private int num;
    private View customView;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_vicnity_run;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        init();
    }


    public void init() {

        mRunAdapter = new VicinityRunAdapter(runList);
        mRvVicinityRun.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvVicinityRun.setAdapter(mRunAdapter);
        customView = VicinityRunDataEngine.getCustomFooterView(getActivity());
        mRunAdapter.addFooterView(customView, 0);
        mSvSendRun.setType(SpringView.Type.FOLLOW);
        mSvSendRun.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                runList.clear();
                pageNumber = 1;
                getSendVicinityRun();
            }

            @Override
            public void onLoadmore() {
                getSendVicinityRun();
            }
        });
        mSvSendRun.setHeader(new HelperHeads(getActivity()));
        mSvSendRun.setFooter(new DefaultFooter(getActivity()));
        getSendVicinityRun();
    }


    private void getSendVicinityRun() {

        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getSendVicinityRun(AppModel.getInstance().getAppLocationEntity().getLats(),
                        AppModel.getInstance().getAppLocationEntity().getLongs(), pageNumber + "")
                .compose(RxHelper.<NearRunEntity>defaultHandleResult())
                .subscribe(new RxSubscribe<NearRunEntity>() {
                    @Override
                    protected void onSucc(NearRunEntity nearRunEntity) {
                        stopLoadingDialog();
                        pageNumber++;
                        num = nearRunEntity.getNum();
                        VicinityRunDataEngine.setTips(num);
                        updateList(nearRunEntity.getRunList());
                        mSvSendRun.onFinishFreshAndLoad();
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        mSvSendRun.onFinishFreshAndLoad();
                    }
                }));

    }

    public void updateList(List<RunEntity> runs) {
        if (runs.size() > 0 && pageNumber >= 2) {
            for (RunEntity run : runs) {
                runList.add(run);
            }
            mRunAdapter.notifyDataSetChanged();
        } else if (runs.size() <= 0 && pageNumber > 2) {
            pageNumber--;
            ToastUtil.showToast("没有更多数据了!");
        }
    }


}
